package com.yufei.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yufei.common.R;
import com.yufei.entity.Category;
import com.yufei.entity.Employee;
import com.yufei.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author zyf
 * @desc 关于分类模块的接口
 */
@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询全部分类
     * @param pageSize
     * @param page
     * @return
     */
    @GetMapping("/category/page")
    public R<Page<Category>> getCategoryPage(Integer pageSize, Integer page){
        //分页对象
        Page<Category> pageInfo = new Page<>(page, pageSize);
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort");
        pageInfo = categoryService.page(pageInfo, wrapper);
        return R.success(pageInfo);
    }

    /**
     * 添加种类或/套餐
     * @param request
     * @param category
     * @return
     */
    @PostMapping("/category")
    public R<String> addCategory(HttpServletRequest request, @RequestBody Category category){
        /**
         * 获取添加信息
         */
        category.setCreateTime(new Date());
        category.setUpdateTime(new Date());
        Long employee1 = (Long) request.getSession().getAttribute("employee");
        category.setCreateUser(employee1);
        category.setUpdateUser(employee1);
        /**
         * 执行添加
         */
        boolean isok = categoryService.save(category);
        if(isok) {
            return R.success("添加成功！");
        }
        return R.error("添加失败");
    }

    /**
     * 编辑种类
     * @param request
     * @param category
     * @return
     */
    @PutMapping("/category")
    public R<String> editCategory(HttpServletRequest request, @RequestBody Category category){
        /**
         * 获取修改信息
         */
        Long employee1 = (Long) request.getSession().getAttribute("employee");
        category.setUpdateUser(employee1);
        category.setUpdateTime(new Date());
        /**
         * 执行更新操作
         */
        boolean isok = categoryService.updateById(category);
        if(isok) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }
    @DeleteMapping("/category")
    public R<String> deleteCategory(Long id){
        boolean isok = categoryService.removeById(id);
        if(isok) {
            return R.success("删除成功！");
        }
        return R.error("删除失败");
    }
    /**
     * 获取类别接口,根据不同type类型获取不同种类目录
     */
    @GetMapping("/category/list")
    public R<List<Category>> getCategoryList(Integer type){
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("type",type);
        List<Category> list = categoryService.list(wrapper);
        return R.success(list);
    }
}
